package cn.com.huzo.service.trade.impl;

import cn.com.huzo.model.entities.trade.CommercialInvoiceLinesEntity;
import cn.com.huzo.repository.trade.CommercialInvoiceLinesRepository;
import cn.com.huzo.repository.trade.CommercialInvoiceLinesWizardRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.trade.CommercialInvoiceLinesManager;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售发票明细管理服务对象类实现。
 *
 * @author 范学春
 */
@Service("CommercialInvoiceLinesManager")
public class CommercialInvoiceLinesManagerImpl extends GenericManagerImpl<CommercialInvoiceLinesEntity, String>
        implements CommercialInvoiceLinesManager {

    @Resource(name = "CommercialInvoiceLinesRepository")
    private CommercialInvoiceLinesRepository _dao;

    @Resource(name = "CommercialInvoiceLinesWizardRepository")
    private CommercialInvoiceLinesWizardRepository _dao_wizard;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public List<CommercialInvoiceLinesEntity> fetchLinesByHeaderKey(Long headerKey) throws Exception {
        return _dao.fetchByHeaderKey(headerKey);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> fetchWizardSearchResult(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();
        _result.put("SalesContractList", _dao_wizard.fetchSalesContractForPage(criterion));
        _result.put("ShipOrderList", _dao_wizard.fetchShipOrderHeaderForPage(criterion));
        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public List<CommercialInvoiceLinesEntity> fetchByWizardResult(Map<String, Object> criterion) throws Exception {
        return _dao.fetchByWizardResult(criterion);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchUpdate(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();
        Integer rowsAffected1 = 0;
        Integer rowsAffected2 = 0;

        ObjectMapper mapper = new ObjectMapper();
        List<CommercialInvoiceLinesEntity> insertList = mapper.convertValue(MapUtils.getObject(criterion, "InsertList"), new TypeReference<List<CommercialInvoiceLinesEntity>>() {
        });
        List<CommercialInvoiceLinesEntity> updateList = mapper.convertValue(MapUtils.getObject(criterion, "UpdateList"), new TypeReference<List<CommercialInvoiceLinesEntity>>() {
        });

        if (!insertList.isEmpty()) {
            rowsAffected1 = _dao.batchInsert(insertList);
        }
        if (!updateList.isEmpty()) {
            rowsAffected2 = _dao.batchUpdate(updateList);
        }

        // 审计日志
        if ((rowsAffected1 + rowsAffected2) > 0) {
            // 记录日志
            //writeAuditLog(itemList.get(0).getUpdatedBy(), "采购管理→非材料花费明细",
            //        "款号：" + _header.getStyleNo(),
            //        "更新");
            _result.put("Success", true);
        } else {
            _result.put("Success", false);
        }

        _result.put("LinesList", _dao.fetchByHeaderKey(MapUtils.getLong(criterion, "HeaderId")));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchDelete(List<CommercialInvoiceLinesEntity> itemList) throws Exception {
        Integer rowsAffected = _dao.batchDelete(itemList);
        if (rowsAffected > 0) {
            // 审计日志
            //writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"), "贸易信息→销售发票明细",
            //        "明细信息：" + MapUtils.getString(criterion, "DeletedLinesInfo"),
            //        "批量删除");
        }
        Map<String, Object> _result = new HashMap();
        _result.put("LinesList", _dao.fetchByHeaderKey(itemList.get(0).getHeaderId()));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        Integer rowsAffected = _dao.delete(MapUtils.getString(criterion, "LinesId"));
        if (rowsAffected > 0) {
            // 审计日志
            writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"), "贸易信息→销售发票明细",
                    "明细信息：" + MapUtils.getString(criterion, "DeletedLinesInfo"),
                    "删除");
        }
        Map<String, Object> _result = new HashMap();
        _result.put("LinesList", _dao.fetchByHeaderKey(MapUtils.getLong(criterion, "HeaderId")));

        return _result;
    }
}
